#include <stdlib.h>
#include <boost/bind.hpp>
#include "../mars/count_down_latch.h"
#include "../mars/thread_pool.h"
#include "../mars/logger.h"

#define TASK_NUM 10

mars::CountDownLatch latch(TASK_NUM);

void task(int task_id) {
    // 耗时任务
    srand(time(0));
    int wait_time = 1 + rand() % 5;
    sleep(wait_time);
    INFO_PRINT("name:%s task:%d time:%d\n", mars::CurrentThread::name(), task_id, wait_time);
    latch.count_down();
}

int main() {
    mars::ThreadPool pool;
    pool.start(5);
    for (int i = 0; i < TASK_NUM; ++i) {
        pool.run(boost::bind(task, i));
    }

    INFO_PRINT("name:%s wait task...\n", mars::CurrentThread::name());
    latch.wait();
    INFO_PRINT("name:%s all task finished\n", mars::CurrentThread::name());
    return 0;
}
